package com.woniuxy.enjoylife.interceptors;

import com.woniuxy.enjoylife.entity.Log;
import com.woniuxy.enjoylife.entity.Role;
import com.woniuxy.enjoylife.mapper.LogMapper;
import com.woniuxy.enjoylife.mapper.UserMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class LogInterceptor extends HandlerInterceptorAdapter {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private LogMapper logMapper;

    @Resource
    private UserMapper userMapper;


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

//        String role = null;
//        role = request.getHeader("role");
//        if (null != role && role.length() != 0) {
//            String userRoles = (String) redisTemplate.opsForValue().get(role);
//            if (null != userRoles && userRoles.length() != 0){
//                if (userRoles.contains("平台")){
//                    if (handler instanceof HandlerMethod) {
//                        HandlerMethod handlerMethod = (HandlerMethod) handler;
//                        Method method = handlerMethod.getMethod();
//                        String requestURI = request.getRequestURI();
//                        Log log =new Log();
//                        String token = request.getHeader("token");
//                        int id = (int)redisTemplate.opsForValue().get(token);
//                        log.setUid(id);
//                        log.setTime(LocalDateTime.now());
//                        log.setContent(requestURI);
//                        mapper.insertLog(log);
//                    }
//                }
//            }
//        }

//        try {
//            int i = (int) request.getSession().getAttribute("id");
//            List<Role> roleList = userMapper.selectUserRole(i);
//            String userRoles = roleList.toString();
//            if (null != userRoles && userRoles.length() != 0) {
//                if (userRoles.contains("平台")) {
//                    if (handler instanceof HandlerMethod) {
//                        HandlerMethod handlerMethod = (HandlerMethod) handler;
//                        Method method = handlerMethod.getMethod();
//                        String requestURI = request.getRequestURI();
//                        Log log = new Log();
//                        log.setUid(i);
//                        log.setTime(LocalDateTime.now());
//                        log.setContent(requestURI);
//                        logMapper.insertLog(log);
//                    }
//                }
//            }
//
//        } catch (Exception e) {
//
//        }

        super.postHandle(request, response, handler, modelAndView);
    }


}
